home *** CD-ROM | disk | FTP | other *** search
/ BMUG Revelations / BMUG Revelations.toast / Utilities / Network / QUE / Basic / quesend.c < prev   
C/C++ Source or Header  |  1989-08-03  |  5KB  |  321 lines

  1. /*
  2. ------------------------------------------------------------
  3. Copyright © 1989 Rob Kassel, Mike McCandless and the Massachusetts Institute of Technology
  4.  
  5. Permission to use, copy, modify, and distribute this software and its documentation for any purpose without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the name of M.I.T. not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission.  The authors and M.I.T. make no representations about the suitability of this software for any purpose.  It is provided “as is” without express or implied warranty.
  6. ------------------------------------------------------------
  7. */
  8.  
  9. #include "mail.h"
  10. #define BUFSZE 5000
  11.  
  12. char theFrom[MAXLINE];
  13. char adress[MAXLINE];
  14. int ptrCnt=0,chrCnt=0;
  15. FILE *fpBBuf;
  16. char* bufPtrs[MAXLINE];
  17.  
  18. main(argc,argv) 
  19. int argc;
  20. char *argv[];
  21.  
  22. {
  23. char pLine[MAXLINE],pName[20],*aBuf;
  24. char sndMl[MAXLINE];
  25. int done,nxtMsg,nxtRdMsg,lineNum;
  26. int Mode;
  27.  
  28. Mode = FALSE;
  29. *theFrom = '\0';
  30.  
  31. if (argc > 2) {
  32.     printf("Usage: %s [-a]\n",argv[0]);
  33.     exit(1);
  34. }
  35.  
  36.  
  37. if (argc == 2)
  38.     if (strcmp(argv[1],"-a")) {
  39.         printf("Usage: %s [-a]\n",argv[0]);
  40.         exit(1);
  41.     }
  42.     else
  43.         Mode = TRUE;    
  44.  
  45.     
  46. bufPtrs[0] = (char*) malloc(BUFSZE * sizeof(char));
  47. if (bufPtrs[0] == NULL) {
  48.     printf("Out of memory.\n");
  49.     exit(1);
  50. }
  51. *(bufPtrs[0]) = '\0';
  52.  
  53.  
  54. if (!Mode) {
  55.     fpBBuf = fopen("bodbuf","w");
  56.     if (fpBBuf == NULL) {
  57.         printf("Couldn't open buffer file.\n");
  58.         exit(1);
  59.     }
  60. }
  61.  
  62. strcpy(adress,readAdress());
  63.  
  64. if (!Mode) {
  65.     readHdr();
  66.     putline(adress);
  67.     putline("\n\0");
  68.  
  69.     while (getline(pLine,MAXLINE) != NULL)
  70.         sendCorrected(pLine);
  71.  
  72.     fclose(fpBBuf);
  73. }
  74.  
  75. else {
  76.     printf("Sending copy...\n");
  77.     transcribe(adress);
  78. }
  79.  
  80. writePipe();
  81. }
  82.  
  83.  
  84.  
  85.  
  86. sendCorrected(pLine)
  87.  
  88. char pLine[MAXLINE];
  89. {
  90. char lineCp[MAXLINE];
  91.  
  92. /*All checking occurs here!!*/
  93.  
  94. strncpy(lineCp,pLine,MAXLINE);
  95.  
  96. if (strncmp("From ",lineCp,5) == 0) {
  97.     lineCp[0] = '>';
  98.     lineCp[1] = '\0';
  99.     strncat(lineCp,pLine,MAXLINE-2);
  100.     if (strlen(lineCp) >= MAXLINE)
  101.         printf("Warning: line too long, data was lost.");
  102. }
  103.  
  104. putline(lineCp);
  105. fputs(lineCp,fpBBuf);
  106. }
  107.  
  108.  
  109.  
  110.  
  111. char* readAdress() {
  112.  
  113. char ch,c;
  114. char buf[MAXLINE],*pBuf;
  115. char buf2[MAXLINE],*pBuf2;
  116. char buf3[MAXLINE + 5];
  117. int aSpce;
  118. int n,x,y;
  119.  
  120. pBuf = buf;
  121. n = 0;
  122.  
  123. while(n < MAXLINE && (c=getchar()) != (char)12) {
  124.     *pBuf++ = c;
  125.     n++;
  126. }
  127.     
  128. c = getchar();
  129.  
  130. if (n == MAXLINE) 
  131.     printf("Data lost in address.\n");
  132.  
  133. *pBuf = '\0';
  134.  
  135.  
  136. pBuf = rmSpce(buf);
  137.  
  138. x = strlen(pBuf);
  139. y = 0;
  140.  
  141. while(y < x) {
  142.     if (*(pBuf + y) == (char) 9 ||  *(pBuf + y) == (char) 10)
  143.         *(pBuf + y) = (char) 32;
  144.     y++;
  145. }
  146.  
  147.  
  148.  
  149. y = 0;
  150. c = 0;
  151. aSpce = FALSE;
  152. pBuf2 = buf2;
  153.  
  154. while (y < x) {
  155.     if (*(pBuf + y) != 32) {
  156.         *(pBuf2 + c) = *(pBuf + y); 
  157.         c++;
  158.         aSpce = FALSE;
  159.     }
  160.     else
  161.         if (!aSpce) {
  162.             *(pBuf2 + c) = *(pBuf + y);
  163.             aSpce = TRUE;
  164.             c++;
  165.         }    
  166.     y++;
  167. }
  168.  
  169. *(pBuf2 + c) = 0;
  170.  
  171.  
  172. y = 0;
  173. x = strlen(pBuf2);
  174.  
  175. while (y < x)  {
  176.     if (*(pBuf2 + y) == ' ')   *(pBuf2 + y) = '.';
  177.     y++;
  178. }
  179.  
  180. sprintf(buf3,"To: %s\n",pBuf2);
  181. return(buf3);
  182. }
  183.  
  184.  
  185.  
  186.  
  187.  
  188. readHdr() {
  189.  
  190. FILE *fpBuf;
  191. char pLine[MAXLINE];
  192.  
  193. fpBuf = fopen("hdrbuf","w");
  194.  
  195. if (fpBuf == NULL) {
  196.     printf("Couldn't open buffer file.\n");
  197.     exit(1);
  198. }
  199.  
  200.  
  201. while (strlen(getline(pLine,MAXLINE)) != 1) {
  202.     if(!strncmp(pLine,"From:",5)) {
  203.         fputs(formatIt(pLine),fpBuf);
  204.         putline(formatIt(pLine));
  205.         strcpy(theFrom,formatIt(pLine) + 6);
  206.         *(theFrom + strlen(theFrom) - 1) = '\0';
  207.     }
  208.     else {
  209.         fputs(pLine,fpBuf);
  210.         putline(pLine);
  211.     }
  212. }
  213.  
  214. fprintf(fpBuf,"-*-%s\n",theFrom);
  215. fclose(fpBuf);
  216. }
  217.  
  218.  
  219. char* formatIt(s)
  220.  
  221. char* s;
  222.  
  223. {
  224. char *m;
  225. char *t;
  226.  
  227.  
  228. t = alcCh(MAXLINE); 
  229. m = (char*) (s + 6);
  230. m = alter(rmSpce(m),'_');
  231. sprintf(t,"From: %s\n",m);
  232.  
  233. return(t);
  234. }
  235.  
  236.  
  237. transcribe(ad)
  238.  
  239. char* ad;
  240.  
  241.  
  242. {
  243. FILE *hdrIn,*bodIn;
  244. char* t;
  245. char buf[MAXLINE];
  246.  
  247. hdrIn = fopen("hdrbuf","r");
  248. bodIn = fopen("bodbuf","r");
  249.  
  250. if (hdrIn == NULL || bodIn == NULL) {
  251.     printf("Can't find previous message.\n");
  252.     exit(1);
  253. }
  254.  
  255. while (TRUE) {
  256.     t = fgets(buf,MAXLINE,hdrIn);
  257.         if (t == NULL)
  258.                 break;
  259.     if (strncmp(buf,"-*-",3) == 0) {
  260.         strcpy(theFrom,buf+3);
  261.         *(theFrom + strlen(theFrom) - 1) = '\0';
  262.         /*get rid of the carriage return*/
  263.     }
  264.     else
  265.         putline(buf);
  266. }
  267.  
  268. putline(ad);
  269. putline("\n\0");
  270.  
  271. while (TRUE) {
  272.     t = fgets(buf,MAXLINE,bodIn);
  273.         if (t == NULL)
  274.                 break;
  275.     putline(buf);
  276. }
  277.  
  278. fclose(hdrIn);
  279. fclose(bodIn);
  280. }
  281.  
  282.  
  283. putline(s)
  284.  
  285. char s[];
  286. {
  287.  
  288. if (chrCnt + strlen(s) > BUFSZE) {
  289.     ptrCnt++;
  290.     chrCnt = 0;
  291.     bufPtrs[ptrCnt] = (char*) malloc(BUFSZE * sizeof(char));
  292.     if (bufPtrs[ptrCnt] == NULL) {
  293.         printf("Out of memory.\n\n");
  294.         exit(1);
  295.     }
  296.     *bufPtrs[ptrCnt] = '\0';
  297. }
  298.  
  299. chrCnt+=strlen(s);
  300. strcat(bufPtrs[ptrCnt],s);
  301. }
  302.  
  303.  
  304. writePipe() {
  305.  
  306. FILE *popen(),*fpOut;
  307. char sndMl[250];
  308. int lp;
  309.  
  310.  
  311. sprintf(sndMl,"sendmail -f %s -t-oem-oi-om\n",theFrom);
  312. fpOut = popen(sndMl,"w");
  313.  
  314. for(lp=0;lp<=ptrCnt;lp++) 
  315.     fprintf(fpOut,"%s",bufPtrs[lp]);
  316.  
  317. pclose(fpOut);
  318. exit(0);
  319.  
  320. }
  321.